const helper = require("../../../utils/helper.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgCount: 0,
    tempFilePaths: [],
    ltCount: 0  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const temp = helper.gettingStorage('TEMPFILEPATHS_INFO');
    this.setData({
      imgCount: temp.imgCount,
      tempFilePaths: temp.tempFilePaths,
      type: temp.type,
      ltCount: temp.imgCount - temp.tempFilePaths.length
    })
  },

  deleteImg: function (e) {
    const imgIndex = e.currentTarget.dataset.imgIndex;
    let {
      imgCount,
      tempFilePaths
    } = this.data;
    tempFilePaths.splice(imgIndex, 1);
    this.setData({
      tempFilePaths,
      ltCount: imgCount - tempFilePaths.length
    })
  },

  chooseImgs: function (e) {
    const self = this;
    let {
      imgCount,
      tempFilePaths,
      ltCount
    } = this.data;
    wx.chooseImage({
      count: ltCount,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const temps = res.tempFilePaths;
        tempFilePaths.push(...temps);
        self.setData({
          tempFilePaths,
          ltCount: imgCount - tempFilePaths.length
        })
      }
    })
  },

  toPage: function (e) {
    const {
      tempFilePaths,
      type,
      imgCount
    } = this.data;
    helper.settingStorage('TEMPFILEPATHS_INFO', { tempFilePaths, type, imgCount});
    wx.navigateTo({
      url: `/pages/user/profile/index?fromPage=${'chooseImgs'}`,
    })
  }
})